# coding:utf-8

import os
import time


class Phone:
    # 手机电源开关
    def getPhoneInfo(self, device='10HDCR0697003GL'):
        phone_state = {}
        adb_msg = os.popen('adb -s %s shell dumpsys window policy' % device)
        phone_msg = adb_msg.buffer.read().decode(encoding='utf-8')
        phone_msg = phone_msg.replace(" ", "")
        array = phone_msg.split("\r\n")
        for item in array:
            kv = item.split("=")
            if len(kv) == 2:
                phone_state.update({kv[0]: kv[1]})

        return phone_state

    def getScreenState(self):
        info = self.getPhoneInfo()
        if info["screenState"] == "SCREEN_STATE_OFF":
            # 息屏
            return 0
        if info["screenState"] == "SCREEN_STATE_ON" and info["showing"] == "true":
            # 亮屏
            return 1
        if info["screenState"] == "SCREEN_STATE_ON" and info["showing"] == "false":
            # 解锁
            return 2

    def makesureScreenOn(self):
        state = self.getScreenState()
        if state == 0:
            # 点亮屏幕
            os.system("adb shell input keyevent 26")
            time.sleep(0.5)
            # 解锁
            os.system("adb shell input swipe 360 1600 360 800 500")
            print("手机亮屏并解锁！")
        if state == 1:
            # 解锁
            os.system("adb shell input swipe 360 1600 360 800 500")
            print("手机解锁！")
        if state == 2:
            print("手机已解锁！")
